/* 
    Copyright (c) 2011 Microsoft Corporation.  All rights reserved.
    Use of this sample source code is subject to the terms of the Microsoft license 
    agreement under which you licensed this sample source code and is provided AS-IS.
    If you did not accept the terms of the license agreement, you are not authorized 
    to use this sample source code.  For the terms of the license, please see the 
    license agreement between you and Microsoft.
  
    To see all Code Samples for Windows Phone, visit http://go.microsoft.com/fwlink/?LinkID=219604 
  
*/
using System;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace PivotApp1
{
    public partial class EventListView : PhoneApplicationPage
    {

        private AppSettings settings = new AppSettings();

        public Visibility idvisible = Visibility.Collapsed; 
        

        private ObservableCollection<EventTable> _tremoloItems;
        public ObservableCollection<EventTable> TremoloItems

        {
            get
            {
                return _tremoloItems;
            }
            set
            {
                if (_tremoloItems != value)
                {
                    _tremoloItems = value;
                    NotifyPropertyChanged("TremoloItems");
                }
            }
        }

        private TremoloDataContext TremoloDB;

        public EventListView()
        {
            InitializeComponent();

            // Add an Application Bar that has a 'done' confirmation button and 
            // a 'cancel' button
          

            TremoloDB = new TremoloDataContext(TremoloDataContext.DBConnectionString);

            if (MainPage.userlevel == 0)
            {

                var allEvents = from EventTable ra in TremoloDB.TremoloEventTable select ra;
                 TremoloItems = new ObservableCollection<EventTable>(allEvents);

            }
            else
            {
                DataTemplate t = eventlist.ItemTemplate;

                idvisible = Visibility.Collapsed;

                var user = from UserTable usr in TremoloDB.TremoloUserTable where usr.UserName==MainPage.username select usr;
                List<UserTable> users = user.ToList();

                var singleEvents = from EventTable ra in TremoloDB.TremoloEventTable where ra.UserId==users.Last().UserId select ra;
                TremoloItems = new ObservableCollection<EventTable>(singleEvents);
            }
           



            eventlist.ItemsSource = TremoloItems;

            // Copy the current settings into the text boxes as the new values
            // entered in will not be saved until the user clicks the 'done' button.

        }

        /// <summary>
        /// Done button clicked event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void doneButton_Click(object sender, EventArgs e)
        {

        }

        private void eventlist_doubletap(object sender, EventArgs e)
        {
           
            EventTable data = (sender as ListBox).SelectedItem as EventTable;
            TremoloDB.TremoloEventTable.DeleteOnSubmit(data);
            TremoloItems.Remove(data);
            TremoloDB.SubmitChanges();

        }

        private void slider_ValueChanged(object sender, EventArgs e)
        {
            EventTable data = eventlist.SelectedItem as EventTable;

            Slider s = sender as Slider;

            s.Value = (int)s.Value;

           //  = (int)s.Value;

            if (data != null)
            {

                data.Completion = (int)s.Value;

                TremoloDB.SubmitChanges();
            }

        }

        /// <summary>
        /// Cancel button clicked event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cancelButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
